package org.example.gui.listener;

import org.example.gui.widget.HistoryPickerPanel;
import org.example.gui.widget.HistoryTablePanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * HistoryPickerPanel 的监听器，按钮后会修改 HistoryPickerPanel.instance.date 为 选择月份的月初
 */

public class MonthPickerListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        HistoryPickerPanel panel = HistoryPickerPanel.instance;
        Integer year = (Integer) panel.cbYear.getSelectedItem();
        Integer month = (Integer) panel.cbMonth.getSelectedItem();
        //用SimpleDateFormat获取所选月月初的 Date
        try {
            panel.date = new SimpleDateFormat("yyyy-MM").parse(String.format("%4d-%02d", year, month));
            //更新 HistoryListPanel ,重新获取数据
            HistoryTablePanel.instance.updateData();
        } catch (ParseException e1) {
            e1.printStackTrace();
        }

    }
}
